import { isObject, isPrimitive } from './is';

export function deepCopy(obj, source?) {
  if (!source) {
    source = {};
  }
  for (const key in obj) {
    const value = obj[key];
    if (isObject(value)) {
      source[key] = deepCopy(value);
    } else if (Array.isArray(value)) {
      const arr: any[] = [];
      for (let i = 0; i < value.length; i++) {
        const item = value[i];
        if (isPrimitive(item)) {
          arr.push(item);
        } else {
          arr.push(deepCopy(item));
        }
      }
      source[key] = arr;
    } else {
      source[key] = value;
    }
  }
  return source;
}
